home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / program / jfklib.zip / DLGTEMP.CPP < prev    next >
Text File  |  1991-05-10  |  10KB  |  492 lines

  1. /*
  2.     DLGTEMP.CPP - Zugriff auf DLGTEMPLATE einer DialogBox
  3.     (C) 1991 by Joachim Kainz 'On a mission from Bhudda'
  4. */
  5.     #include "dlgtemp.hpp"
  6.     #include <string.h>
  7. /*
  8.     Tools zum Bearbeiten des Resource
  9.     ___________________________________________________________________
  10. */
  11.     static LPSTR CopyResourcePtr    (             LPSTR  lpPtr);
  12.     static BOOL  FreeResourcePtr    (             LPSTR &lpPtr);
  13.     static BYTE  GetResourceByte    (             LPSTR &lpDlg);
  14.     static int      GetResourceInt        (             LPSTR &lpDlg);
  15.     static long  GetResourceLong    (             LPSTR &lpDlg);
  16.     static LPSTR GetResourcePtr        (             LPSTR &lpDlg);
  17.     static WORD  GetResourcePtrSize    (             LPSTR &lpPtr);
  18.     static void  SetResourceByte    (PSTR &pDlg, BYTE byValue);
  19.     static void     SetResourceInt        (PSTR &pDlg, int   nValue);
  20.     static void  SetResourceLong    (PSTR &pDlg, long  lValue);
  21.     static void     SetResourcePtr        (PSTR &pDlg, LPSTR  lpPtr);
  22.  
  23.     inline int GetResourceInt (LPSTR & lpDlg)
  24.     {
  25.         int nResult = *(int far *) lpDlg;
  26.  
  27.         lpDlg += sizeof (int);
  28.  
  29.         return nResult;
  30.     }
  31.  
  32.     inline BYTE GetResourceByte (LPSTR & lpDlg)
  33.     {
  34.         BYTE byResult = *(BYTE far *) lpDlg;
  35.  
  36.         lpDlg  += sizeof (BYTE);
  37.  
  38.         return byResult;
  39.     }
  40.  
  41.     inline long GetResourceLong (LPSTR & lpDlg)
  42.     {
  43.         long lResult = *(long far *) lpDlg;
  44.  
  45.         lpDlg  += sizeof (long);
  46.  
  47.         return lResult;
  48.     }
  49.  
  50.     LPSTR GetResourcePtr (LPSTR &lpDlg)
  51.     {
  52.         LPSTR lpResult;
  53.  
  54.         if (*lpDlg == -1) {
  55.  
  56.             lpResult = MAKEINTRESOURCE (*(WORD far *) (++lpDlg));
  57.             lpDlg    += sizeof (int);
  58.  
  59.             return lpResult;
  60.  
  61.         }
  62.  
  63.         WORD wLen = lstrlen (lpDlg);
  64.  
  65.         if (!wLen) {
  66.  
  67.             lpDlg++;
  68.             return NULL;
  69.  
  70.         }
  71.  
  72.         lpResult = new char [wLen+1];
  73.  
  74.         if (!lpResult)
  75.             return (LPSTR) -1l;
  76.  
  77.         _fmemmove (lpResult, lpDlg, wLen+1);
  78.  
  79.         lpDlg += wLen+1;
  80.  
  81.         return lpResult;
  82.     }
  83.  
  84.     BOOL FreeResourcePtr (LPSTR & lpPtr)
  85.     {
  86.         if (!lpPtr)
  87.             return TRUE;
  88.  
  89.         if (!HIWORD (lpPtr)) {
  90.  
  91.             lpPtr = NULL;
  92.             return TRUE;
  93.  
  94.         }
  95.  
  96.         delete (PSTR) lpPtr;
  97.  
  98.         lpPtr = NULL;
  99.  
  100.         return TRUE;
  101.     }
  102.  
  103.     WORD GetResourcePtrSize (LPSTR & lpPtr)
  104.     {
  105.         if (!lpPtr)
  106.             return 1;
  107.  
  108.         if (!HIWORD (lpPtr))
  109.             return 3;
  110.  
  111.         return lstrlen (lpPtr)+1;
  112.     }
  113.  
  114.  
  115.     LPSTR CopyResourcePtr (LPSTR lpPtr)
  116.     {
  117.         if (!lpPtr)
  118.             return NULL;
  119.  
  120.         if (!HIWORD (lpPtr))
  121.             return lpPtr;
  122.  
  123.         WORD  wLen  = lstrlen (lpPtr);
  124.         LPSTR lpNew = new char [wLen+1];
  125.  
  126.         if (!lpNew)
  127.             return (LPSTR) -1l;
  128.  
  129.         _fmemmove (lpNew, lpPtr, wLen+1);
  130.  
  131.         return lpNew;
  132.     }
  133.  
  134.     void SetResourcePtr (PSTR &pDlg, LPSTR lpPtr)
  135.     {
  136.         if (!lpPtr) {
  137.  
  138.             *(pDlg++) = '\0';
  139.             return;
  140.  
  141.         }
  142.  
  143.         if (!HIWORD (lpPtr)) {
  144.  
  145.             *(            pDlg ++) = -1;
  146.             *(((WORD *) pDlg)++) = LOWORD (lpPtr);
  147.  
  148.             return;
  149.  
  150.         }
  151.  
  152.         WORD wMem = lstrlen (lpPtr)+1;
  153.  
  154.         _fmemmove (pDlg, lpPtr, wMem);
  155.  
  156.         pDlg += wMem;
  157.     }
  158.  
  159.     inline void SetResourceByte (PSTR &pDlg, BYTE byValue)
  160.     {
  161.         *(pDlg++) = byValue;
  162.     }
  163.  
  164.     inline void SetResourceInt     (PSTR &pDlg, int nValue)
  165.     {
  166.         *(((int *)pDlg)++) = nValue;
  167.     }
  168.  
  169.     inline void SetResourceLong (PSTR &pDlg, long lValue)
  170.     {
  171.         *(((long *)pDlg)++) = lValue;
  172.     }
  173. /*
  174.     DLGTEMPLATE
  175.     ___________________________________________________________________
  176. */
  177.     EXPORT DLGTEMPLATE::DLGTEMPLATE (LPSTR lpDlgTemp)
  178.     {
  179.         _fmemset (this, 0, sizeof *this);
  180.  
  181.         HANDLE hDlg = FindResource (
  182.                         GetInstance (),
  183.                         lpDlgTemp,
  184.                         RT_DIALOG
  185.                       );
  186.  
  187.         if (!hDlg)
  188.             return;
  189.  
  190.         hDlg = LoadResource (GetInstance (), hDlg);
  191.  
  192.         if (!hDlg)
  193.             return;
  194.  
  195.         LPSTR lpDlg = LockResource (hDlg);
  196.  
  197.         if (!lpDlg) {
  198.  
  199.             hDlg = FreeResource (hDlg);
  200.             return;
  201.  
  202.         }
  203.  
  204.         int nItemCount;
  205.  
  206.         lStyle          = GetResourceLong (lpDlg);
  207.         nItemCount      = GetResourceByte (lpDlg);
  208.         x              = GetResourceInt    (lpDlg);
  209.         y              = GetResourceInt    (lpDlg);
  210.         cx              = GetResourceInt    (lpDlg);
  211.         cy              = GetResourceInt    (lpDlg);
  212.         lpMenuName      = GetResourcePtr    (lpDlg);
  213.         lpClassName   = GetResourcePtr    (lpDlg);
  214.         lpCaptionText = GetResourcePtr    (lpDlg);
  215.  
  216.         if (lStyle & DS_SETFONT) {
  217.  
  218.             font.nSize        = GetResourceInt (lpDlg);
  219.             font.lpFaceName    = GetResourcePtr (lpDlg);
  220.  
  221.         }
  222.  
  223.         pdtit = new DLGITEMTEMPLATE [nItemCount];
  224.  
  225.         for (int m=0; m<nItemCount; m++)
  226.             if (!pdtit [m].ConvertResource (lpDlg))
  227.                 break;
  228.  
  229.         UnlockResource (hDlg);
  230.  
  231.         hDlg = FreeResource (hDlg);
  232.  
  233.         byItemCount = m;
  234.  
  235.         bValid = m                ==    nItemCount    &&
  236.                  lpMenuName        != (LPSTR) -1l    &&
  237.                  lpClassName    != (LPSTR) -1l    &&
  238.                  lpCaptionText    != (LPSTR) -1l;
  239.     }
  240.  
  241.     EXPORT DLGTEMPLATE::~DLGTEMPLATE ()
  242.     {
  243.         if (byItemCount) {
  244.  
  245.             delete [byItemCount] pdtit;
  246.             byItemCount = 0;
  247.  
  248.         }
  249.  
  250.         FreeResourcePtr (      lpMenuName   );
  251.         FreeResourcePtr (      lpClassName  );
  252.         FreeResourcePtr (      lpCaptionText);
  253.         FreeResourcePtr (font.lpFaceName   );
  254.  
  255.         bValid = FALSE;
  256.     }
  257.  
  258.     BOOL EXPORT DLGTEMPLATE::SetClass (LPSTR lpNewClsName)
  259.     {
  260.         FreeResourcePtr (lpClassName);
  261.  
  262.         lpClassName = CopyResourcePtr (lpNewClsName);
  263.  
  264.         return TRUE;
  265.     }
  266.  
  267.     WORD EXPORT DLGTEMPLATE::GetSize ()
  268.     {
  269.         WORD wSize = sizeof             (long          )+
  270.                      sizeof             (BYTE          )+
  271.                      sizeof             (int           )*4+
  272.                      GetResourcePtrSize (lpMenuName   )+
  273.                      GetResourcePtrSize (lpClassName  )+
  274.                      GetResourcePtrSize (lpCaptionText);
  275.  
  276.         if (lStyle & DS_SETFONT)
  277.             wSize += sizeof             (short int        )+
  278.                      GetResourcePtrSize (font.lpFaceName);
  279.  
  280.         for (int m=0; m<byItemCount; m++)
  281.             wSize += pdtit [m].GetSize ();
  282.  
  283.         return wSize;
  284.     }
  285.  
  286.     PSTR EXPORT DLGTEMPLATE::operator PSTR ()
  287.     {
  288.         WORD wResSize    = GetSize ();
  289.         PSTR pComponent    = new char [wResSize],
  290.              pResource    = pComponent;
  291.  
  292.         if (!pComponent)
  293.             return NULL;
  294.  
  295.         SetResourceLong (pComponent, lStyle          );
  296.         SetResourceByte (pComponent, byItemCount  );
  297.         SetResourceInt    (pComponent, x              );
  298.         SetResourceInt    (pComponent, y              );
  299.         SetResourceInt    (pComponent, cx              );
  300.         SetResourceInt    (pComponent, cy              );
  301.         SetResourcePtr    (pComponent, lpMenuName   );
  302.         SetResourcePtr    (pComponent, lpClassName  );
  303.         SetResourcePtr    (pComponent, lpCaptionText);
  304.  
  305.         if (lStyle & DS_SETFONT) {
  306.  
  307.             *(((int  *) pComponent)++) = font.nSize;
  308.             SetResourcePtr (pComponent, font.lpFaceName);
  309.  
  310.         }
  311.  
  312.         for (int m=0; m<byItemCount; m++)
  313.             if (!pdtit [m].MakeResource (pComponent)) {
  314.  
  315.                 break;
  316.                 delete pResource;
  317.  
  318.                 return NULL;
  319.  
  320.             }
  321.  
  322.         return pResource;
  323.     }
  324.  
  325.     WORD EXPORT DLGITEMTEMPLATE::GetSize ()
  326.     {
  327.         WORD wSize = sizeof             (int    )*5+
  328.                      sizeof             (long    )+
  329.                      GetResourcePtrSize (lpClass)+
  330.                      GetResourcePtrSize (lpText )+
  331.                      sizeof             (BYTE    )+
  332.                      byInfo;
  333.  
  334.         return wSize;
  335.     }
  336.  
  337.     BOOL EXPORT DLGITEMTEMPLATE::MakeResource (PSTR &pDlg)
  338.     {
  339.         SetResourceInt    (pDlg, x        );
  340.         SetResourceInt    (pDlg, y        );
  341.         SetResourceInt    (pDlg, cx        );
  342.         SetResourceInt    (pDlg, cy        );
  343.         SetResourceInt    (pDlg, nID        );
  344.         SetResourceLong    (pDlg, lStyle    );
  345.         SetResourcePtr    (pDlg, lpClass    );
  346.         SetResourcePtr    (pDlg, lpText    );
  347.         SetResourceByte    (pDlg, byInfo    );
  348.  
  349.         if (byInfo) {
  350.  
  351.             memmove (pDlg, pData, byInfo);
  352.  
  353.             pDlg += byInfo;
  354.  
  355.         }
  356.  
  357.         return TRUE;
  358.     }
  359.  
  360.     BOOL EXPORT DLGITEMTEMPLATE::ConvertResource (LPSTR &lpDlg)
  361.     {
  362.         if (!Reset ())
  363.             return FALSE;
  364.  
  365.         x        = GetResourceInt    (lpDlg);
  366.         y        = GetResourceInt    (lpDlg);
  367.         cx        = GetResourceInt    (lpDlg);
  368.         cy        = GetResourceInt    (lpDlg);
  369.         nID        = GetResourceInt    (lpDlg);
  370.         lStyle    = GetResourceLong    (lpDlg);
  371.         lpClass    = GetCtlClass        (lpDlg);
  372.         lpText    = GetResourcePtr    (lpDlg);
  373.         byInfo    = GetResourceByte    (lpDlg);
  374.  
  375.         if (byInfo) {
  376.  
  377.             pData = new char [byInfo];
  378.  
  379.             _fmemmove (pData, lpDlg, byInfo);
  380.  
  381.             lpDlg += byInfo;
  382.  
  383.         }
  384.  
  385.         return TRUE;
  386.     }
  387.  
  388.     #define CTL_BUTTON        128
  389.     #define CTL_EDIT        129
  390.     #define CTL_STATIC        130
  391.     #define CTL_LISTBOX        131
  392.     #define CTL_COMBOBOX    133
  393.  
  394.     LPSTR DLGITEMTEMPLATE::GetCtlClass (LPSTR &lpDlg)
  395.     {
  396.         if (*lpDlg >= 0)
  397.             return GetResourcePtr (lpDlg);
  398.  
  399.         PSTR pClass = new char [10];
  400.  
  401.         switch ((BYTE) *lpDlg) {
  402.  
  403.           case CTL_BUTTON:
  404.             lstrcpy (pClass, "button");
  405.             break;
  406.  
  407.           case CTL_EDIT:
  408.             lstrcpy (pClass, "edit");
  409.             break;
  410.  
  411.           case CTL_STATIC:
  412.             lstrcpy (pClass, "static");
  413.             break;
  414.  
  415.           case CTL_LISTBOX:
  416.             lstrcpy (pClass, "listbox");
  417.             break;
  418.  
  419.           case CTL_COMBOBOX:
  420.             lstrcpy (pClass, "combobox");
  421.             break;
  422.  
  423.         }
  424.  
  425.         ++lpDlg;
  426.  
  427.         return pClass;
  428.     }
  429.  
  430.     BOOL DLGITEMTEMPLATE::Reset ()
  431.     {
  432.         if (byInfo)
  433.             delete pData;
  434.  
  435.         return    FreeResourcePtr (lpClass) &&
  436.                 FreeResourcePtr (lpText );
  437.     }
  438.  
  439.     EXPORT DLGITEMTEMPLATE::~DLGITEMTEMPLATE ()
  440.     {
  441.         Reset ();
  442.     }
  443.  
  444.     EXPORT DLGITEMTEMPLATE::DLGITEMTEMPLATE ()
  445.     {
  446.         _fmemset (this, 0, sizeof *this);
  447.     }
  448.  
  449.     EXPORT DLGITEMTEMPLATE::DLGITEMTEMPLATE (const DLGITEMTEMPLATE &dit)
  450.     {
  451.         CopyDlgItem (dit);
  452.     }
  453.  
  454.     DLGITEMTEMPLATE EXPORT DLGITEMTEMPLATE::operator = (const DLGITEMTEMPLATE &dit)
  455.     {
  456.         CopyDlgItem (dit);
  457.  
  458.         return *this;
  459.     }
  460.  
  461.     BOOL DLGITEMTEMPLATE::CopyDlgItem (const DLGITEMTEMPLATE &dit)
  462.     {
  463.         x        = dit.x;
  464.         y        = dit.y;
  465.         cx        = dit.cx;
  466.         cy        = dit.cy;
  467.         nID        = dit.nID;
  468.         lStyle    = dit.lStyle;
  469.         lpClass    = CopyResourcePtr (dit.lpClass);
  470.         lpText    = CopyResourcePtr (dit.lpText );
  471.         byInfo    = dit.byInfo;
  472.  
  473.         if (byInfo) {
  474.  
  475.             pData = new char [byInfo];
  476.  
  477.             if (!pData)
  478.                 return FALSE;
  479.  
  480.             _fmemmove (pData, dit.pData, byInfo);
  481.  
  482.         }
  483.  
  484.         return    lpClass    != (LPSTR) -1l &&
  485.                 lpText    != (LPSTR) -1l;
  486.     }
  487.  
  488.     DLGITEMTEMPLATE EXPORT DLGTEMPLATE::operator [] (WORD wItem)
  489.     {
  490.         return pdtit [wItem];
  491.     }
  492.